package com.hot100.easy;

/**
 * @ClassName Demo09
 * @Description 买股票
 * @Author zt    0,0,4,0,5,0
 *                0,0,4,4,5,5
 *                0,
 * @Date 17:01 2022/6/9
 * @Version 1.0
 **/
public class Demo09 {


    public static void main(String[] args) {

    }

    public int maxProfit(int[] prices) {
        if(prices.length<=1){
            return 0;
        }
        int[] nums=new int[prices.length-1];
        for (int i = 0; i <nums.length ; i++) {
            nums[i]=prices[i+1]-prices[i];
        }
        return maxSubArray2(nums);
    }

    /**
     * 方法一超时了
     */
    public  static int maxSubArray2(int[] nums) {
        int[] newNums=new int[nums.length];
        newNums[0]=nums[0];
        int max=nums[0];
        for (int i = 1; i <nums.length; i++) {
            newNums[i]=Math.max(newNums[i-1]+nums[i],nums[i]);
            max=Math.max(max,newNums[i]);
        }
        return max>0?max:0;
    }
}